package com.hmall.pay.web;

import com.hmall.common.client.OrderClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.User;
import com.hmall.pay.pojo.RequestParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("payok")
public class PayController {
    @Autowired
    private OrderClient orderClient;
    @Autowired
    private UserClient userClient;
    @GetMapping("/status/{id}")
    public int getStatus(@PathVariable("id") Long orderId) {
        return orderClient.getStatus(orderId);
    }

    @PostMapping("/payment")
    @Transactional
    public int payment(@RequestBody RequestParams params) {
        User user = new User();
        user.setId(Long.parseLong(params.getId()));
        user.setPassword(params.getPassword());
        user.setBalance(Long.parseLong(params.getTotalFee().toString()));
        int i = orderClient.payisOk(Long.valueOf(params.getOrderId()));
        int i1 = userClient.updateBalance(user);
        if (i==1&&i1==1){
            return 1;
        }else {
            return 0;
        }

    }
}
